package hhm.android.library.datePicker.adapter.datetime;

import androidx.annotation.NonNull;

import java.util.Calendar;

import hhm.android.library.datePicker.bean.DateParams;
import hhm.android.library.datePicker.bean.DatePick;

public class DayAdapter extends DatePickAdapter {

    public DayAdapter(@NonNull DateParams dateParams, @NonNull DatePick datePick, int selectColor) {
        super(dateParams, datePick, selectColor);
    }

    @Override
    public int getCurrentIndex() {
        return mData.indexOf(mDatePick.day);
    }

    @Override
    public String getItem(int position) {
        int number = mData.get(position);
        return number + "日";
    }

    @Override
    public void refreshValues() {
        Calendar calendar = Calendar.getInstance();

        calendar.setTime(mDateParams.currentDate);
        int currentYear = calendar.get(Calendar.YEAR);
        int currentMonth = calendar.get(Calendar.MONTH);

        if (mDatePick.year == currentYear && currentMonth + 1 == mDatePick.month) {
            int endDay = calendar.get(Calendar.DAY_OF_MONTH);
            setData(getArray(endDay));
        } else {
            calendar.set(mDatePick.year, mDatePick.month - 1, 1);

            int day = mDatePick.day;
            int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

            mDatePick.day = Math.min(day, maxDay);

            setData(getArray(maxDay));
        }
    }
}
